//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.example.service;

import cn.hutool.core.util.StrUtil;
import com.example.entity.Account;
import com.example.entity.Admin;
import com.example.exception.CustomException;
import com.example.mapper.AdminMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import java.util.Iterator;
import java.util.List;
import org.springframework.stereotype.Service;

@Service
public class AdminService {
    @Resource
    private AdminMapper adminMapper;

    public AdminService() {
    }

    public void add(Admin admin) { //管理员不需要注册只需要新增，只有管理员才能增加管理员
        String username = admin.getUsername();
        Admin dbadmin = this.adminMapper.selectByUsername(username);
        if (dbadmin != null) {
            throw new CustomException("500", "用户名已存在，请更换其他用户名");
        } else {
            if (StrUtil.isBlank(admin.getPassword())) {
                admin.setPassword("admin"); //默认密码 admin
            }

            if (StrUtil.isBlank(admin.getName())) {
                admin.setName(admin.getUsername());
            }

            admin.setRole("AMD");//必须设置role
            this.adminMapper.insert(admin);
        }
    }

    public void updateById(Admin admin) {
        this.adminMapper.updateById(admin);
    }

    public void deleteById(Integer id) {
        this.adminMapper.deleteById(id);
    }

    public void deleteBatch(List<Integer> ids) {
        Iterator var2 = ids.iterator();

        while(var2.hasNext()) {
            Integer id = (Integer)var2.next();
            this.deleteById(id);
        }

    }

    public Admin selectById(Integer id) {
        return this.adminMapper.selectById(id);
    }

    public List<Admin> selectAll(Admin admin) {
        return this.adminMapper.selectAll(admin);
    }

    public List<Admin> selectList(Admin admin) {
        System.out.println(admin);
        return null;
    }

    public PageInfo<Admin> selectPage(Admin admin, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Admin> list = this.adminMapper.selectAll(admin);
        return PageInfo.of(list);
    }

    public Admin login(Account account) {
        String username = account.getUsername();
        Admin dbadmin = this.adminMapper.selectByUsername(username);
        if (dbadmin == null) {
            throw new CustomException("500", "用户不是管理员");
        } else {
            String password = account.getPassword();
            if (!dbadmin.getPassword().equals(password)) {
                throw new CustomException("500", "密码错误，请重新输入");
            } else {
                return dbadmin;
            }
        }
    }

    public void updatePassword(Account account) {
        Integer id = account.getId();
        Admin admin = this.selectById(id);
        if (!admin.getPassword().equals(account.getPassword())) {
            throw new CustomException("500", "原密码错误");
        } else {
            admin.setPassword(account.getNewPassword());
            this.updateById(admin);
        }
    }
}
